package com.kuang.demo;

import com.kuang.pojo.person;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;

public class session01 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; Charset =utf-8");
        PrintWriter out = resp.getWriter();

        HttpSession session = req.getSession();
        session.setAttribute("username","汪小平");
        session.setAttribute("person",new person("秦疆",20));

        String id = session.getId();

        if(session.isNew()){
            out.write("session创建成功");
        }else {
            out.write("session已经存在，为："+id);
        }

        //session创建的时候做了什么？
//        Cookie cookie = new Cookie("JSESSIONID", id);
//        resp.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
